package terra.twl.window;

import terra.Loader;
import terra.managers.DefManager;
import terra.model.ItemClientDef;
import terra.network.packet.client.RequestDropItem;
import terra.slick.GameState;
import terra.twl.RootPane;
import terra.twl.widget.ItemSlot;
import de.matthiasmann.twl.ThemeInfo;
import de.matthiasmann.twl.Widget;

/**
 *
 * @author Matthias Mann
 */
public class InventoryPanel extends Widget implements IDropable{
    
    private int numSlotsX;
    private int numSlotsY;
    private final ItemSlot[] slot;
    private int slotSpacing;

    private ItemSlot dragSlot;
    private ItemSlot dropSlot;
    
    public InventoryPanel(int numSlotsX, int numSlotsY) {
        this.numSlotsX = numSlotsX;
        this.numSlotsY = numSlotsY;
        this.slot = new ItemSlot[numSlotsX * numSlotsY];

        for(int i=0 ; i<slot.length ; i++) {
            slot[i] = new ItemSlot();
            slot[i].setListener(this);
            add(slot[i]);
        }
    }
    
    public void update(){
    	for(int i = 0 ; i < slot.length;i++){
    		slot[i].reset();
    	}
    	for(ItemClientDef def : GameState.getInstance().inventory.getInventoryItems().values()){
    		ItemSlot slot = containsItem(def.getOId());
    		if (slot == null)
    			getFirstEmpty().setDef(def);
    		else
    			slot.setDef(def);
    	}
    }
    
    ItemSlot getFirstEmpty(){
    	for(int i = 0 ; i < slot.length;i++){
    		if (slot[i].def == null)
    			return slot[i];
    	}
    	return null;
    }
    ItemSlot containsItem(int oId){
    	for(int i = 0 ; i < slot.length;i++){
    		if (slot[i].def != null && slot[i].def.getOId() == oId)
    			return slot[i];
    	}
    	return null;
    }
    
    @Override
    public int getPreferredInnerWidth() {
        return (slot[0].getPreferredWidth() + slotSpacing)*numSlotsX - slotSpacing;
    }

    @Override
    public int getPreferredInnerHeight() {
        return (slot[0].getPreferredHeight() + slotSpacing)*numSlotsY - slotSpacing;
    }

    @Override
    protected void layout() {
        int slotWidth  = slot[0].getPreferredWidth();
        int slotHeight = slot[0].getPreferredHeight();
        
        for(int row=0,y=getInnerY(),i=0 ; row<numSlotsY ; row++) {
            for(int col=0,x=getInnerX() ; col<numSlotsX ; col++,i++) {
                slot[i].adjustSize();
                slot[i].setPosition(x, y);
                x += slotWidth + slotSpacing;
            }
            y += slotHeight + slotSpacing;
        }
    }

    @Override
    protected void applyTheme(ThemeInfo themeInfo) {
        super.applyTheme(themeInfo);
        slotSpacing = themeInfo.getParameter("slotSpacing", 5);
    }
    
    public void dragStarted(ItemSlot slot,int x,int y) {
        if(slot.def != null) {
            dragSlot = slot;
            dragging(slot, x,y);
        }
    }
    
    public void dragging(ItemSlot slot,int x,int y) {
        if(dragSlot != null) {
            Widget w = getWidgetAt(x,y);
            if(w instanceof ItemSlot) {
                setDropSlot((ItemSlot)w);
            } else {
                setDropSlot(null);
            }
        }
    }
    
    public void dragStopped(ItemSlot slot, int x,int y,Object param,Object param2) {
        if(dragSlot != null) {
            dragging(slot,x,y);
            if(dropSlot != null && dropSlot.canDrop() && dropSlot != dragSlot) {
                dropSlot.setDef(dragSlot.def);
                dragSlot.reset();
            } else if (dropSlot == null){
	        	Widget w = GameState.getInstance().getRootPane().getWidgetAt(x,y);
	        	System.out.println(this + " dragStopped on widget: "+ w + " parent " + w.getParent());

	        	if (w == null || w instanceof RootPane) {
	    			if (slot.def.getCount() == 1 || !DefManager.allItems.get(slot.def.getId()).isStackable) {
	    				RequestDropItem request = new RequestDropItem(GameState.getInstance().player.getObjectId(),slot.def.getOId(), slot.def.getId(), x,y,1);
	    				Loader.handler.send(request);
	    			} else {
	    				GameState.getInstance().countchooser.init(slot,x,y,null,this);
	    			}
	        	} else if (w instanceof ItemSlot && w.getParent() != this && w.getParent() instanceof IDropable){
	        		System.out.println("awdawdwd");
	        		//((IDropable) w.getParent()).dragStopped(slot,evt,this,1);
	        		if (slot.def.getCount() == 1 || !DefManager.allItems.get(slot.def.getId()).isStackable)
	        			((IDropable) w.getParent()).dragStopped(slot,x,y,this,1);
	        		else{
	        			System.out.println("!!!111awdawdwd");
	        			if (GameState.getInstance().altPressed())
	        				((IDropable) w.getParent()).dragStopped(slot,x,y,this,slot.def.getCount());
	        			else
	        				GameState.getInstance().countchooser.init(slot,x,y,(IDropable) w.getParent(),this);
	        		}
	        		//System.out.println("this "+ slot + "   to " + w + "  parent " + w.getParent());
	        	}
            }
        }
    setDropSlot(null);
    dragSlot = null;
    }

    
    
    private void setDropSlot(ItemSlot slot) {
        if(slot != dropSlot) {
            if(dropSlot != null) {
                dropSlot.setDropState(false, false);
            }
            dropSlot = slot;
            if(dropSlot != null) {
                dropSlot.setDropState(true, dropSlot == dragSlot || dropSlot.canDrop());
            }
        }
    }
}
